package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.BookBo;
import com.groupseven.happycampus.common.pojo.Book;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.vo.AuthorBarVo;
import com.groupseven.happycampus.common.vo.BookVo;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.common.vo.SpeciesPieVo;
import com.groupseven.happycampus.mapper.BookMapper;
import com.groupseven.happycampus.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @CreateTime: 2022-07-27 11:19
 * @author: bestzqw
 * @Description:
 */
@Service("bookService")
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public LayuiPageVo<Book> bookList(Map<String, String> params) {


        System.out.println(params);
        //解析参数
        Integer startPageNum = Integer.parseInt(params.get("page"));
        Integer size = Integer.parseInt(params.get("limit"));
        PageHelper.startPage(startPageNum,size);

        List<Book> books = bookMapper.bookList(params);

        //将查询得到的分页数据放在 PageInfo 中
        PageInfo<Book> pageInfo = new PageInfo<>(books);

        LayuiPageVo<Book> layuiPage = new LayuiPageVo<>();
        //开始封装分页数据
        layuiPage.setCode(0);
        layuiPage.setMsg("分页列表数据");
        layuiPage.setCount(pageInfo.getTotal());
        layuiPage.setData(books);

        return layuiPage;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int deleteAll(String idstr) {
        return bookMapper.deleteAll(idstr);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int updateBook(Book book) {
        return bookMapper.updateBook(book);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int addBook(Book book) {
        return bookMapper.addBook(book);
    }

    @Override
    public List<BookBo> bookExcelData(Book book) {
        return bookMapper.bookExcelData(book);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int addBooks(List<Book> list) {
        return bookMapper.addBooks(list);
    }

    @Override
    public List<AuthorBarVo> selectByAuthorBar() {
        return bookMapper.selectByAuthorBar();
    }

    @Override
    public List<SpeciesPieVo> selectBySpeciesPie() {
        return bookMapper.selectBySpeciesPie();
    }

    @Override
    public List<BookVo> bookGroup() {
        return bookMapper.bookGroup();
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public int borrowBook(Integer id) {

        Book book = bookMapper.selectBookById(id);
        Integer local =  book.getResidueNumber();
        Integer rent = book.getRentNumber();
        if(local == null){ // 为null时包空指针异常
            local=0;
        }

        if (rent == null){  // 解决rent 为null时包空指针异常
            rent = 0;
        }
        if (local>0){
            book.setResidueNumber(local-1);
            book.setRentNumber(rent+1);
            int i = bookMapper.updateBook(book);
            return i;
        }else {
            return 0;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public int restoreBook(Integer id) {
        Book book = bookMapper.selectBookById(id);
        Integer local =  book.getResidueNumber();
        Integer rent = book.getRentNumber();
        if(local == null){
            local=0;
        }

        if (rent == null){  // 解决rent 为null时包空指针异常
            rent = 0;
        }

        if (rent>0){
            book.setRentNumber(rent-1);
            book.setResidueNumber(local+1);
            int i = bookMapper.updateBook(book);
            return i;
        }else {
            return 0;
        }

    }


}
